package org.huoran.sakamichi.main.netty.simpleTCP;

import org.apache.commons.lang3.StringUtils;
import org.jboss.netty.channel.*;

public class TCPClientHandler extends SimpleChannelUpstreamHandler  {
    private static final String CLOSE_FLAG = "exit";

    @Override
    public void channelConnected(ChannelHandlerContext ctx, ChannelStateEvent e) {
//        e.getChannel().write("this is client.");
    }

    @Override
    public void messageReceived(ChannelHandlerContext ctx, MessageEvent e) {
        String content = e.getMessage().toString();
        if(StringUtils.isNotEmpty(content) && content.equals(CLOSE_FLAG)){
            e.getChannel().close();
        }
    }

    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, ExceptionEvent e) {
        e.getCause().printStackTrace();
        e.getChannel().close();
    }
}